package com.hainatong.jcmvvmbase.login.fragment;


import android.view.View;
import android.view.ViewGroup;
import androidx.recyclerview.widget.LinearLayoutManager;
import com.blankj.utilcode.util.ToastUtils;
import com.hainatong.jcmvvmbase.MvvmLazyFragment;
import com.hainatong.jcmvvmbase.R;
import com.hainatong.jcmvvmbase.databinding.FragmentListViewBinding;
import com.hainatong.jcmvvmbase.login.adapter.LanguageAdapter;
import com.hainatong.jcmvvmbase.login.entity.LanguageSelectedModel;
import com.hainatong.mvvmbase.base.NoViewModel;

import java.util.ArrayList;
import java.util.List;

public class LanguageFragment extends MvvmLazyFragment<FragmentListViewBinding, NoViewModel>
{
    private LanguageAdapter adapter;
    private List<LanguageSelectedModel> languageModels;
    @Override
    public int getLayoutId()
    {
        return R.layout.fragment_list_view;
    }

    @Override
    public int getBindingVariable() {
        return 0;
    }

    @Override
    protected void onRetryBtnClick()
    {

    }

    @Override
    protected void initView(View view)
    {
        adapter = new LanguageAdapter();
        languageModels = new ArrayList<LanguageSelectedModel>();
        viewDataBinding.recyclerview.setHasFixedSize( true);
        viewDataBinding.recyclerview.setLayoutManager( new LinearLayoutManager(getActivity()));

        View driver = getLayoutInflater().inflate(R.layout.driver_dp10, (ViewGroup) viewDataBinding.recyclerview.getParent(),false);
        adapter.addHeaderView( driver );

        viewDataBinding.recyclerview.setAdapter( adapter );

        adapter.setOnItemClickListener( (adapter1, view1, position) -> {

            ToastUtils.showShort("选择 -> "+position );

            for(int i=0;i<languageModels.size();i++  )
            {
                if( i == position )
                {
                    languageModels.get(position).setSelected( true );
                }
                else
                {
                    languageModels.get(position).setSelected( false );
                }
            }
            adapter.notifyDataSetChanged();
        } );


    }

    @Override
    protected void lazyLoad()
    {
        LanguageSelectedModel zh = new LanguageSelectedModel();
        zh.setLanguage( "中文（默认）" );
        zh.setLanguageType( "zh/中文" );
        zh.setSelected( false );
        languageModels.add( zh );

        LanguageSelectedModel en = new LanguageSelectedModel();
        en.setLanguage( "英文" );
        en.setLanguageType( "English/英文" );
        en.setSelected( true );
        languageModels.add( en );

        adapter.setList( languageModels );


    }
}
